package com.wwwarehouse.taskcenter.adapter.job_point;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;

import com.wwwarehouse.common.adapter.media.CommonAdapter;
import com.wwwarehouse.common.adapter.media.ViewHolder;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.bean.job_point.QualityInfoResponseBean;
import com.wwwarehouse.taskcenter.eventbus_event.JobPointStateEvent;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by Jackie on 2017/10/30.
 * 子作业点品质维度  选择添加方式  查看所有 详情列表  适配器
 */

public class QualityAdapter extends CommonAdapter<QualityInfoResponseBean> {
    private List<QualityInfoResponseBean> mQualityDetailsList;
    private int mItemHeight;

    public QualityAdapter(Context context, int layoutId, List<QualityInfoResponseBean> qualityDetailsList, int itemHeight) {
        super(context, layoutId, qualityDetailsList);

        this.mQualityDetailsList = qualityDetailsList;
        this.mItemHeight = itemHeight;
    }

    @Override
    public void convert(ViewHolder holder, final QualityInfoResponseBean qualityDetailsBean) {
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params.height = mItemHeight;
        holder.getConvertView().setLayoutParams(params);

        final ImageView selectImg = holder.getView(R.id.iv_select);

        holder.getConvertView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //单选
                qualityDetailsBean.setSelect(!qualityDetailsBean.isSelect());

                for (QualityInfoResponseBean qualityInfoResponseBean : mQualityDetailsList) {
                    if (qualityDetailsBean.isSelect() && qualityDetailsBean.getQualityUkid() != null &&
                            !qualityDetailsBean.getQualityUkid().equals(qualityInfoResponseBean.getQualityUkid())) {
                        //有一个选中的话，其他的都重置
                        qualityInfoResponseBean.setSelect(false);
                    }
                }

                notifyDataSetChanged();

                EventBus.getDefault().post(new JobPointStateEvent());
            }
        });

        holder.setText(R.id.tv_name, qualityDetailsBean.getName());

        if (qualityDetailsBean.isSelect()) {
            selectImg.setVisibility(View.VISIBLE);
        } else {
            selectImg.setVisibility(View.GONE);
        }
    }
}
